package com.cx.controller;

import com.cx.pojo.wjr.PriceReturn;
import com.cx.util.wjr.GetDistance;
import com.cx.util.wjr.PriceSelect;
import com.cx.vo.ResponseResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("page")
public class PageSelectController {

    @ResponseBody
    @RequestMapping("selectByArea")
    public ResponseResult selectByArea(String start,String end){

        ResponseResult responseResult = new ResponseResult();
        String statCoordinate = null;
        String endCoordinate = null;

        if(start.contains("日本")|| start.contains("韩国") || start.contains("新加坡") || start.contains("越南")){
            responseResult.setCode(2);
            responseResult.setMessage("无效的地址");
            return responseResult;
        }
        if(end.contains("日本")|| end.contains("韩国") || start.contains("新加坡") || start.contains("越南")){
            responseResult.setCode(2);
            responseResult.setMessage("无效的地址");
            return responseResult;
        }

        try {
            statCoordinate = GetDistance.getLonLat(start);
            endCoordinate = GetDistance.getLonLat(end);
            Long dis = GetDistance.getDistance(statCoordinate,endCoordinate);
            int firstPrice = PriceSelect.firstMoney(dis);
            int continuationPrice = PriceSelect.Continuation(dis);
            PriceReturn priceReturn = new PriceReturn();
            priceReturn.setFirstPrice(firstPrice);
            priceReturn.setContinuation(continuationPrice);
            priceReturn.setSumPrice(firstPrice + continuationPrice);
            responseResult.setData(priceReturn);
        }catch (NullPointerException e){
            responseResult.setCode(2);
            responseResult.setMessage("无效的地址");
            return responseResult;
        }catch (Exception e){
            responseResult.setCode(2);
            responseResult.setMessage("未知异常");
            return responseResult;
        }
        System.out.println(responseResult);
        responseResult.setCode(1);
        responseResult.setMessage("计算成功");
        return responseResult;
    }

    @ResponseBody
    @RequestMapping("selectByAreaAndWeight")
    public ResponseResult selectByAreaAndWeight(String start,String end,Integer weight){
        System.out.println("selectA&W方法执行了");
        ResponseResult responseResult = new ResponseResult();
        String statCoordinate = null;
        String endCoordinate = null;

        if(start.contains("日本")|| start.contains("韩国") || start.contains("新加坡") || start.contains("越南")){
            responseResult.setCode(2);
            responseResult.setMessage("无效的地址");
            return responseResult;
        }
        if(end.contains("日本")|| end.contains("韩国") || start.contains("新加坡") || start.contains("越南")){
            responseResult.setCode(2);
            responseResult.setMessage("无效的地址");
            return responseResult;
        }

        try {
            statCoordinate = GetDistance.getLonLat(start);
            endCoordinate = GetDistance.getLonLat(end);
            Long dis = GetDistance.getDistance(statCoordinate,endCoordinate);
            int firstPrice = PriceSelect.firstMoney(dis);
            int continuationPrice = PriceSelect.Continuation(dis);
            int sum = firstPrice + (continuationPrice * (weight - 1));
            PriceReturn priceReturn = new PriceReturn();
            priceReturn.setFirstPrice(firstPrice);
            priceReturn.setContinuation(continuationPrice);
            priceReturn.setSumPrice(sum);
            responseResult.setData(priceReturn);
        }catch (NullPointerException e){
            responseResult.setCode(2);
            responseResult.setMessage("无效的地址");
            return responseResult;
        }catch (Exception e){
            responseResult.setCode(2);
            responseResult.setMessage("未知异常");
            return responseResult;
        }
        System.out.println(responseResult);
        responseResult.setCode(1);
        responseResult.setMessage("计算成功");
        return responseResult;
    }
}
